﻿using CefSharp.Wpf;
using PidKeyReptile.Desktop.CrawlTask;
using PidKeyReptile.Desktop.QuartzTask;
using Quartz;
using Quartz.Impl;
using Stylet;
using StyletIoC;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Quartz.Logging.OperationName;

namespace PidKeyReptile.Desktop.Pages.Crawler
{
    public class CrawlerManagerViewModel : Screen
    {
        private IScheduler scheduler;//声明Quartz调度程序实例，用与管理Job

        private string _logText;

        public string LogText
        {
            get { return _logText; }
            set { SetAndNotify(ref _logText, value); }
        }

        public CrawlerManagerViewModel()
        {

        }

        protected override void OnPropertyChanged(string propertyName)
        {
            if (propertyName == nameof(LogText))
            {
                if (_logText.Length > 5000)
                {
                    _logText = "";
                }

            }
            base.OnPropertyChanged(propertyName);
        }

        private IScheduler _scheduler;

        public void TaskRun()
        {
            var thisObj = new Dictionary<string, CrawlerManagerViewModel>();
            thisObj.Add("Crawler", this);

            //Quartz3.0版本初始化方式1
            scheduler = StdSchedulerFactory.GetDefaultScheduler().GetAwaiter().GetResult();


            //创建AIhao
            IJobDetail job_aihao = JobBuilder.Create<AihaoSyncjob>().UsingJobData(new JobDataMap(thisObj)).WithIdentity("aihao").Build();
            ITrigger aihao_trigger = TriggerBuilder.Create()
               .WithIdentity("aihao")
               .StartNow()
               .WithSimpleSchedule(x => x.WithIntervalInSeconds(60 * 5).RepeatForever())
               .Build();//创建trigger 现在开始 每10秒执行一次  无限重复
            scheduler.ScheduleJob(job_aihao, aihao_trigger);//scheduler关联trigger和job

            //创建Jieke
            IJobDetail job_jike = JobBuilder.Create<JikeSyncJob>().UsingJobData(new JobDataMap(thisObj)).WithIdentity("jieke").Build();
            ITrigger jieke_trigger = TriggerBuilder.Create()
               .WithIdentity("jieke")
               .StartNow()
               .WithSimpleSchedule(x => x.WithIntervalInSeconds(60 * 5).RepeatForever())
               .Build();//创建trigger 现在开始 每10秒执行一次  无限重复
            scheduler.ScheduleJob(job_jike, jieke_trigger);//scheduler关联trigger和job

            scheduler.Start();//启动scheduler
        }

        /// <summary>
        /// 暂停
        /// </summary>
        public void TaskStop()
        {

        }
    }
}
